# -*- coding: utf-8 -*-
"""
Date: 2022-01-12
Author: J z
"""
from framework.dal.repository_impl import RoleRepository
from framework.redisutils.redis_storage import RedisStorage
from utils.share_rank_utils import ShareRankManger


class GameRank(ShareRankManger):

    """
    排行榜
    """

    def __init__(self, join_scale=100000000000):
        self.redis = RedisStorage.redis_conn_map['lock']
        self.join_scale = join_scale

    def get_range_detail(self, rank_type, start=0, end=99):
        rank_data_list = self.get_range(rank_type, start, end)
        rids = [p[0] for p in rank_data_list]
        roles = RoleRepository.filter_by_rid_list(rids)
        # 构造映射
        role_dict = {role.role_guid:role for role in roles}
        # 转换排行榜单格式
        rank = 1
        rank_list = []
        for rank_info in rank_data_list:
            # 格式:排名/头像/昵称/分数
            r_role_guid, r_score = rank_info[0], rank_info[1]
            r_role = role_dict.get(r_role_guid)
            if not r_role:
                continue
            rank_list.append([rank, r_role.head, r_role.nick_name, r_score])
            rank += 1
        return rank_list


game_rank = GameRank()
